Tool use with Claude
Claude is capable of interacting with external client-side tools and functions
How tool use works
1. Provide Claude with tools and a user prompt
2. Claude decides to use a tool
3. Extract tool input, run code, and return results
4. Claude uses tool result to formulate a response
Tools are user-provided
Claude does not have access to any built-in server-side tools.
The computer use (beta) functionality is an exception
How to implement tool use
Choosing a model
Generally, use Claude 3.7 Sonnet, Claude 3.5 Sonnet or Claude 3 Opus for complex tools and ambiguous queries
複数のツールを扱える
Use Claude 3.5 Haiku or Claude 3 Haiku for straightforward tools
引数を忘れるかも
Specifying tools
name
description
input_schema
Tool use system prompt
When you call the Anthropic API with the tools parameter, we construct a special system prompt from the tool definitions, tool configuration, and any user-specified system prompt
code:txt
In this environment you have access to a set of tools you can use to answer the user's question.
{{ FORMATTING INSTRUCTIONS }}
String and scalar parameters should be specified as is, while lists and objects should use JSON format. Note that spaces for string values are not stripped. The output is not expected to be valid XML and is parsed with regular expressions.
Here are the functions available in JSONSchema format:
{{ TOOL DEFINITIONS IN JSON SCHEMA }}
{{ USER SYSTEM PROMPT }}
{{ TOOL CONFIGURATION }}
Best practices for tool definitions
Provide extremely detailed descriptions
Prioritize descriptions over examples
よい例
descriptionが詳しい
input_schemaの引数のdescriptionに例示
The good description clearly explains what the tool does, when to use it, what data it returns, and what the ticker parameter means.
Controlling Claude’s output
Forcing tool use
JSON output
you can use tools anytime you want the model to return JSON output that follows a provided schema.
クライアントサイドの(実際に実行できる)関数でなくてもよい
Chain of thought
Parallel tool use
無効にもできる
Handling tool use and tool result content blocks
When you receive a tool use response, you should:
積ん読
Differences from other APIs
Unlike APIs that separate tool use or use special roles like tool or function, Anthropic’s API integrates tools directly into the user and assistant message structure.
Tool use examples
CoT
JSON mode
Pricing
Tool use system prompt token countで課金される